package com.example.project.key.Dao.employeeDao;

import com.example.project.key.IBaseDao.IBaseDao_employee;
import com.example.project.key.connection.databaseconn;
import com.example.project.key.object.employee.employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class employeeDao implements IBaseDao_employee {
    private static PreparedStatement stmt = null;
    employee e = new employee();
    @Override
    public boolean add(Connection con,String dep, String worknum, String id, String tele, String gender, String mailbox, String age,String username,String pwd) {
        boolean t=false;
        if(con!=null)
        {
            String sql="insert into employee( `department` , `worknum` , `id` , `telenum` , `sex` , `mailbox` , `age` , `username`, `password` ) values(?,?,?,?,?,?,?,?,?)";
            try {
                stmt = con.prepareStatement(sql);
                con.setAutoCommit(false);
                stmt.setString(1,dep);
                stmt.setString(2,worknum);
                stmt.setString(3,id);
                stmt.setString(4,tele);
                stmt.setString(5,gender);
                stmt.setString(6,mailbox);
                stmt.setString(7,age);
                stmt.setString(8,username);
                stmt.setString(9,pwd);
                stmt.addBatch();
                stmt.executeBatch();
                con.commit();
                t=true;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                t=false;
            }

        }else {
            t=false;
        }
        return t;
    }

    @Override
    public boolean delete(Connection con,String id, String password, String username) {
        return false;
    }

    @Override
    public boolean modify(Connection con,String dep, String worknum, String id, String tele, String gender, String mailbox, String age) {
        return false;
    }

    @Override
    public employee find(Connection con, String username, String pwd) {
        if(con!=null)
        {
            String sql="select * from employee where username = ? and password = ?";
            try {
                stmt=con.prepareStatement(sql);
                stmt.setString(1,username);
                stmt.setString(2,pwd);
                ResultSet rs= stmt.executeQuery();
                while (rs.next())
                {
                    e.setAge(rs.getString("age"));
                    e.setPwd(rs.getString("password"));
                    e.setDepartment(rs.getString("department"));
                    e.setWorknum(rs.getString("worknum"));
                    e.setId(rs.getString("id"));
                    e.setSex(rs.getString("sex"));
                    e.setE_mail(rs.getString("mailbox"));
                    e.setTelenum(rs.getString("telenum"));
                    e.setUsername(rs.getString("username"));
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return e;
        }else {
            return null;
        }
    }

}
